package cn.charlotte.pit.util.rank;

import me.clip.placeholderapi.PlaceholderAPI;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.group.GroupManager;
import net.luckperms.api.model.user.User;
import net.luckperms.api.model.user.UserManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class LuckPermsUtil {

    static LuckPerms luckPerms = LuckPermsProvider.get();
    static UserManager userManager = luckPerms.getUserManager();
    static GroupManager groupManager = luckPerms.getGroupManager();


    public LuckPermsUtil() {
    }

    // 不需要依赖LP的prefix，后续接入VIP
    public static String getAllPrefix(UUID uuid) {
        Player player = Bukkit.getPlayer(uuid);
        if (player == null) {
            return "&7";
        }
        return PlaceholderAPI.setPlaceholders(player, "§r%avip_chat%§r%ctitle_prefix2%§7");
    }

    // 不需要依赖LP的prefix，后续接入VIP
    public static String getVipPrefix(UUID uuid) {
        Player player = Bukkit.getPlayer(uuid);
        if (player == null) {
            return "&7";
        }
        return PlaceholderAPI.setPlaceholders(player, "§r%avip_chat%§7");
    }

    public static String getSuffix(UUID uuid) {
        return "§7";
    }

    public static User getUser(UUID uuid) {
        if (userManager == null) {
            return null;
        }

        if (userManager.isLoaded(uuid)) {
            return userManager.getUser(uuid);
        } else {
            CompletableFuture<User> userCompletableFuture = userManager.loadUser(uuid);
            return userCompletableFuture.join();
        }
    }

}
